<?php


namespace App\Http\Controllers\applets\service;


use App\Http\Controllers\applets\model\AdminSnModel;
use App\Http\Controllers\applets\model\AppInstallModel;
use App\Http\Controllers\applets\model\BrowserModel;
use App\Http\Controllers\applets\model\LabelModel;
use App\Http\Controllers\applets\model\ParentSnModel;
use App\Http\Controllers\applets\model\ParentAppModel;
use App\Http\Controllers\applets\model\TeacherAppModel;
use App\Http\Controllers\applets\model\ScreenshotModel;
use App\Http\Controllers\applets\model\SettingModel;
use App\Http\Controllers\applets\model\SnGradeModel;
use App\Http\Controllers\applets\model\TimeControlModel;
use App\Http\Controllers\applets\model\AdminAppModel;
use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;
use Illuminate\Support\Facades\DB;

class SnService
{


    /**
     * 获取浏览器网址或书签信息
     * @param $param
     * @return false|string
     */
    public static function getBrowserUrlInfo($param){

        $data = [];
        $type = (int)$param['type'];
        $num = (int)$param['num'];

        $Browser = BrowserModel::queryFirst(['id'=>$num,'type'=>$type],['address']);
        if($Browser){

            $data['num'] = $num;
            $data['address'] = $Browser['address'];
            $data['type'] = $type;
            
        }else{
            return ToolUtil::returnJson(300,'没有数据');
        }

        return ToolUtil::returnJson(200,'成功',$data);

    }

    /**
     * 增加浏览器网址或书签
     * @param $param
     * @return false|string
     */
    public static function addBrowser($param){

        $type = (int)$param['type'];

        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['value']);
        if(empty($parent['value'])){
            return ToolUtil::returnJson(300,'设备没绑定');
        }

        if($type === 0){

            $browser = DB::table('t_browser')
                ->where(['sn_value'=>$parent['value'],'address'=>$param['address']])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->first(['id']);
            if($browser){
                return ToolUtil::returnJson(301,'不能添加重复网址');
            }

            $desc = '添加黑名单浏览器网址';
            $contentType = 6;

        }else if($type === 1){

            $browser = DB::table('t_browser')
                ->where(['sn_value'=>$parent['value'],'address'=>$param['address']])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->first(['id']);
            if($browser){
                return ToolUtil::returnJson(301,'不能添加重复网址');
            }

            $desc = '添加白名单浏览器网址';
            $contentType = 6;

        }else if($type === 2){

            $browser = BrowserModel::queryFirst(['sn_value'=>$parent['value'],'type'=>2],['id']);
            if($browser){
                return ToolUtil::returnJson(300,'不能重复添加主页');
            }

            $desc = '小程序添加浏览器主页';
            $contentType = 18;

        }else if($type === 3){

            $browser = BrowserModel::queryFirst(['sn_value'=>$parent['value'],'type'=>3,'address'=>$param['address']],['id']);
            if($browser){
                return ToolUtil::returnJson(300,'不能重复添加书签');
            }

            $desc = '小程序添加浏览器书签';
            $contentType = 18;

        }else{
            return ToolUtil::returnJson(300,'类型错误');
        }

        $res = BrowserModel::add(['type'=>$type,'admin_id'=>$param['userId'],'address'=>$param['address'],'sn_value'=>$parent['value'],'create_time'=>time()]);
        if(empty($res)){
            return ToolUtil::returnJson(300,'失败');
        }

        $param['type'] = 2;
        $param['contentType'] = $contentType;
        $param['statusType'] = $desc;
        $param['batch'] = array($parent['value']);
        $param['remark'] = $desc;
        $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
        LogUtil::addAdminLog($param);
        $jPush = LogUtil::addPushLog($param);
        if(in_array(200,$jPush)){
            return ToolUtil::returnJson(200,'成功');
        }else{
            return ToolUtil::returnJson(200,'成功,推送失败');
        }

    }


    /**
     * 编辑浏览器网址或书签
     * @param $param
     * @return false|string
     */
    public static function editBrowser($param){

        $type = (int)$param['type'];
        $num = (int)$param['num'];

        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['value']);
        if(empty($parent['value'])){
            return ToolUtil::returnJson(300,'设备没绑定');
        }

        if($type === 0){

            $browser = DB::table('t_browser')
                ->where(['sn_value'=>$parent['value'],'address'=>$param['address']])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->first(['id']);
            if($browser && $browser['id'] != $num){
                return ToolUtil::returnJson(301,'不能添加重复网址');
            }

            $desc = '编辑黑名单浏览器网址';
            $contentType = 6;

        }else if($type === 1){

            $browser = DB::table('t_browser')
                ->where(['sn_value'=>$parent['value'],'address'=>$param['address']])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->first(['id']);
            if($browser && $browser['id'] != $num){
                return ToolUtil::returnJson(301,'不能添加重复网址');
            }

            $desc = '编辑白名单浏览器网址';
            $contentType = 6;

        }else if($type === 2){

            $browser = BrowserModel::queryFirst(['sn_value'=>$parent['value'],'type'=>2],['id']);
            if($browser && $browser['id'] != $num){
                return ToolUtil::returnJson(300,'不能重复添加主页');
            }

            $desc = '小程序编辑浏览器主页';
            $contentType = 18;

        }else if($type === 3){

            $browser = BrowserModel::queryFirst(['sn_value'=>$parent['value'],'type'=>3,'address'=>$param['address']],['id']);
            if($browser && $browser['id'] != $num){
                return ToolUtil::returnJson(300,'不能重复添加书签');
            }

            $desc = '小程序编辑浏览器书签';
            $contentType = 18;

        }else{
            return ToolUtil::returnJson(300,'类型错误');
        }

        $res = BrowserModel::edit(['id'=>$num,'sn_value'=>$parent['value']],['type'=>$type,'address'=>$param['address'],'update_time'=>time()]);
        if(empty($res)){
            return ToolUtil::returnJson(300,'失败');
        }

        $param['type'] = 2;
        $param['contentType'] = $contentType;
        $param['statusType'] = $desc;
        $param['batch'] = array($parent['value']);
        $param['remark'] = $desc;
        $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
        LogUtil::addAdminLog($param);
        $jPush = LogUtil::addPushLog($param);
        if(in_array(200,$jPush)){
            return ToolUtil::returnJson(200,'成功');
        }else{
            return ToolUtil::returnJson(200,'成功,推送失败');
        }

    }


    /**
     * 删除浏览器网址或书签
     * @param $param
     * @return false|string
     */
    public static function deleteBrowser($param){

        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['value']);
        if(empty($parent['value'])){
            return ToolUtil::returnJson(300,'设备没绑定');
        }

        $browser = BrowserModel::queryFirst(['id'=>$param['num']],['type','address']);
        if(empty($browser)){
            return ToolUtil::returnJson(300,'编号错误');
        }

        if($browser['type'] === 0){

            $desc = '删除浏览器黑名单网址';
            $contentType = 6;

        }else if($browser['type'] === 1){

            $desc = '删除浏览器白名单网址';
            $contentType = 6;

        }else if($browser['type'] === 2){

            $desc = '小程序删除浏览器主页';
            $contentType = 18;

        }else if($browser['type'] === 3){

            $desc = '小程序删除浏览器书签';
            $contentType = 18;

        }else{
            return ToolUtil::returnJson(300,'类型错误');
        }

        $param['type'] = 2;
        $param['batch'] = array($parent['value']);
        $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$browser['address']]);

        if(BrowserModel::delete(['id'=>$param['num']])){

            $param['contentType'] = $contentType;
            $param['remark'] = $desc;
            LogUtil::addAdminLog($param);
            $param['statusType'] = $desc;
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }else{
                return ToolUtil::returnJson(200,'成功,推送失败');
            }

        }

        return ToolUtil::returnJson(300,'失败');

    }


    /**
     * sn编号获取浏览器网址
     * @param $param
     * @return false|string
     */
    public static function getBrowser($param){

        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['value']);
        if($parent){

            $Browser = BrowserModel::queryWhere(['sn_value'=>$parent['value']],['id as num','address','type']);
            if(count($Browser)){
                return ToolUtil::returnJson(200,'成功',$Browser);
            }
        }
        return ToolUtil::returnJson(400,'没有数据');
    }


    /**
     * sn编号获取系统管控
     * @param $param
     * @return false|string
     */
    public static function getSetting($param){
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['value']);
        $data = SettingModel::queryFirst(['sn_value'=>$parent['value']],
            ['id','setting_usb','setting_white_app','setting_bluetooth','setting_bht','setting_tfmedia','setting_memory','setting_camera','setting_call','setting_hotspot','qch_restore','setting_tfmedia','setting_browserInput','dev_mode','setting_tfmedia_format','setting_recording','setting_file','setting_picture','setting_music','search_topic','setting_phone','setting_phones','setting_browser','is_eyecare','eyecare_near','eyecare_lie','eyecare_shake','eyecare_light','eyecare_bw','eyecare_tired','eyecare_tired_val']);
        if($data){
            $tfArr = explode(',',$data['setting_tfmedia_format']);
            $arr = [];
            foreach ($tfArr as $key=>$value){
                $arr[$key] = DB::table('t_tfmedia')->where('id',$value)->first(['format','tfmedia_id']);
            }
            $data['setting_tfmedia_format'] = $arr;
            $data['setting_white_app_value'] = '';
            $white_app = DB::table('t_white_app as w')->leftJoin('t_setting as s','s.sn_value','=','w.sn')->where('w.sn',$parent['value'])->first(['w.package_name']);
            if(!empty($white_app)){
                $data['setting_white_app_value'] = $white_app['package_name'];
            }
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 设置系统管控信息
     * @param $param
     * @return false|string
     */
    public static function editSetting($param){
        try {
            $setting = SettingModel::queryFirst(['id'=>$param['id']],['sn_value']);
            $parent = ParentSnModel::queryWhereFirst(['a.value'=>$setting['sn_value']],['teacher_id']);
            if($parent['teacher_id']){
                return ToolUtil::returnJson(301,'该设备授权老师，不能进行该操作');
            }
            switch ($param['type']){
                case 1:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_usb'=>$param['setting_usb'],'update_time'=>time()]);
                    $type = 1;
                    break;
                case 2:
                    if(!isset($param['setting_bht'])){
                        return ToolUtil::returnJson(301,'缺少setting_bht参数');
                    }
                    if(!isset($param['setting_bluetooth'])){
                        return ToolUtil::returnJson(301,'缺少setting_bluetooth参数');
                    }
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_bht'=>$param['setting_bht'],'setting_bluetooth'=>$param['setting_bluetooth'],'update_time'=>time()]);
                    $type = 2;
                    break;
                case 3:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_memory'=>$param['setting_memory'],'update_time'=>time()]);
                    $type = 3;
                    break;
                case 4:
                    $setting_tfmedia_format = null;
                    $tfArr = explode(',',$param['setting_tfmedia_format']);
                    if($tfArr){
                        $arr = [];
                        foreach ($tfArr as $key=>$value){
                            $t_tfmedia_id = DB::table('t_tfmedia')->where('format',$value)->value('id');
                            if($t_tfmedia_id){
                                $arr[] = $t_tfmedia_id;
                            }
                        }
                        $setting_tfmedia_format = implode(',',$arr);
                    }
                    
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_tfmedia'=>$param['setting_tfmedia'],'setting_tfmedia_format'=>$setting_tfmedia_format,'update_time'=>time()]);
                    $type = 4;
                    break;
                case 5:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_camera'=>$param['setting_camera'],'update_time'=>time()]);
                    $type = 5;
                    break;
                case 6:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_call'=>$param['setting_call'],'update_time'=>time()]);
                    $type = 6;
                    break;
                case 7:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_hotspot'=>$param['setting_hotspot'],'update_time'=>time()]);
                    $type = 7;
                    break;
                case 8:
                    $int = SettingModel::edit(['id'=>$param['id']],['qch_restore'=>$param['qch_restore'],'update_time'=>time()]);
                    $type = 8;
                    break;
                case 10:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_browserInput'=>$param['setting_browserInput'],'update_time'=>time()]);
                    $type = 10;
                    break;
                case 11:
                    $int = SettingModel::edit(['id'=>$param['id']],['dev_mode'=>$param['dev_mode'],'update_time'=>time()]);
                    $type = 11;
                    break;
                case 12:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_recording'=>$param['setting_recording'],'update_time'=>time()]);
                    $type = 12;
                    break;
                case 13:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_file'=>$param['setting_file'],'update_time'=>time()]);
                    $type = 13;
                    break;
                case 14:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_picture'=>$param['setting_picture'],'update_time'=>time()]);
                    $type = 14;
                    break;
                case 15:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_music'=>$param['setting_music'],'update_time'=>time()]);
                    $type = 15;
                    break;
                case 16:
                    $int = SettingModel::edit(['id'=>$param['id']],['search_topic'=>$param['search_topic'],'update_time'=>time()]);
                    $type = 16;
                    break;
                case 17:

                    if(!isset($param['setting_phone'])){
                        return ToolUtil::returnJson(300,'电话白名单开关参数为空');
                    }
                    $setting_phone = (int)$param['setting_phone'];
                    if(empty($param['setting_phones'])){
                        $setting_phones = '';
                    }else{
                        $setting_phones = trim($param['setting_phones']);
                    }
                    
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_phone'=>$setting_phone,'setting_phones'=>$setting_phones,'update_time'=>time()]);
                    $type = 17;
                    break;
                case 18:
                    $int = SettingModel::edit(['id'=>$param['id']],['setting_browser'=>$param['setting_browser'],'update_time'=>time()]);
                    $type = 18;
                    break;
                case 19:
                    if(!isset($param['setting_white_app'])){
                        return ToolUtil::returnJson(301,'缺少setting_white_app参数');
                    }
                    if(!isset($param['setting_white_app_value'])){
                        return ToolUtil::returnJson(301,'缺少setting_white_app_value参数');
                    }
                    $setting_white_app = (int)$param['setting_white_app'];
                    if($setting_white_app === 0){
                        DB::table('t_white_app')->where('sn',$setting['sn_value'])->delete();
                    }else{
                        $white_app_rec = DB::table('t_white_app')->where('sn',$setting['sn_value'])->first(['id']);
                        if(!empty($white_app_rec)){
                            $t_white_app_data = [
                                'package_name'=>$param['setting_white_app_value'],
                                'update_time'=>time()
                            ];
                            DB::table('t_white_app')->where('sn',$setting['sn_value'])->update($t_white_app_data);
                        }else{
                            $t_white_app_data = [
                                'admin_id'=>$param['userId'],
                                'package_name'=>$param['setting_white_app_value'],
                                'batch_id'=>'',
                                'sn'=>$setting['sn_value'],
                                'group'=>4,
                                'create_time'=>time()
                            ];
                            DB::table('t_white_app')->insert($t_white_app_data);
                        }
                    }

                    $int = SettingModel::edit(['id'=>$param['id']],['setting_white_app'=>$param['setting_white_app'],'update_time'=>time()]);
                    $type = 19;
                    break;
                case 20:
                    $int = SettingModel::edit(['id'=>$param['id']],['is_eyecare'=>$param['is_eyecare'],'update_time'=>time()]);
                    $type = 20;
                    break;
                case 21:
                    $int = SettingModel::edit(['id'=>$param['id']],['eyecare_near'=>$param['eyecare_near'],'update_time'=>time()]);
                    $type = 21;
                    break;
                case 22:
                    $int = SettingModel::edit(['id'=>$param['id']],['eyecare_lie'=>$param['eyecare_lie'],'update_time'=>time()]);
                    $type = 22;
                    break;
                case 23:
                    $int = SettingModel::edit(['id'=>$param['id']],['eyecare_shake'=>$param['eyecare_shake'],'update_time'=>time()]);
                    $type = 23;
                    break;
                case 24:
                    $int = SettingModel::edit(['id'=>$param['id']],['eyecare_light'=>$param['eyecare_light'],'update_time'=>time()]);
                    $type = 24;
                    break;
                case 25:
                    $int = SettingModel::edit(['id'=>$param['id']],['eyecare_bw'=>$param['eyecare_bw'],'update_time'=>time()]);
                    $type = 25;
                    break;
                case 26:

                    if(!isset($param['eyecare_tired'])){
                        return ToolUtil::returnJson(300,'疲劳开关参数为空');
                    }
                    $eyecare_tired = (int)$param['eyecare_tired'];
                    if(empty($param['eyecare_tired_val'])){
                        $eyecare_tired_val = 0;
                    }else{
                        $eyecare_tired_val = intval($param['eyecare_tired_val']);
                    }
                    
                    $int = SettingModel::edit(['id'=>$param['id']],['eyecare_tired'=>$eyecare_tired,'eyecare_tired_val'=>$eyecare_tired_val,'update_time'=>time()]);
                    $type = 26;
                    break;


                default :
                    $int = 0;
                    $type = 0;
            }
            if($int){
                $param['status'] = 1;
                $param['type'] = 2;
                $param['batch'] = array($setting['sn_value']);
                $param['title'] = 'OK';
                switch ($type){
                    case 1:
                        $param['contentType'] = 3;
                        $param['remark'] = '数据传输管控';
                        $param['statusType'] = '数据传输管控';
                        $data['setting_usb'] = $param['setting_usb'];
                        break;
                    case 2:
                        $param['contentType'] = 5;
                        $param['remark'] = '蓝牙管控';
                        $param['statusType'] = '蓝牙管控';
                        $data['setting_bluetooth'] = $param['setting_bluetooth'];
                        $data['setting_bht'] = $param['setting_bht'];
                        break;
                    case 3:
                        $param['contentType'] = 4;
                        $param['remark'] = '存储卡传输管控';
                        $param['statusType'] = '存储卡传输管控';
                        $data['setting_memory'] = $param['setting_memory'];
                        break;
                    case 4:
                        $param['contentType'] = 12;
                        $param['remark'] = '影音格式管控';
                        $param['statusType'] = '影音格式管控';
                        $data['setting_tfmedia'] = $param['setting_tfmedia'];
                        if($param['setting_tfmedia']==0){
                            if(!empty($arr)){
                                $data['setting_tfmedia_format'] = $param['setting_tfmedia_format'];
                            }else{
                                $data['setting_tfmedia_format'] = '';
                            }

                            if(!empty($param['setting_tfmedia_format']) && empty($arr)){
                                return ToolUtil::returnJson(300,'影音格式错误');
                            }
                        }
                        break;
                    case 5:
                        $param['contentType'] = 13;
                        $param['remark'] = '摄像头管控';
                        $param['statusType'] = '摄像头管控';
                        $data['setting_camera'] = $param['setting_camera'];
                        break;
                    case 6:
                        $param['contentType'] = 14;
                        $param['remark'] = '电话功能管控';
                        $param['statusType'] = '电话功能管控';
                        $data['setting_call'] = $param['setting_call'];
                        break;
                    case 7:
                        $param['contentType'] = 29;
                        $param['remark'] = 'wifi热点管控';
                        $param['statusType'] = 'wifi热点管控';
                        $data['setting_hotspot'] = $param['setting_hotspot'];
                        break;
                    case 8:
                        $param['contentType'] = 30;
                        $param['remark'] = '恢复出厂管控';
                        $param['statusType'] = '恢复出厂管控';
                        $data['qch_restore'] = $param['qch_restore'];
                        break;
                    case 10:
                        $param['contentType'] = 32;
                        $param['remark'] = '浏览器手动输入管控';
                        $param['statusType'] = '浏览器手动输入管控';
                        $data['setting_browserInput'] = $param['setting_browserInput'];
                        break;
                    case 11:
                        $param['contentType'] = 33;
                        $param['remark'] = '开发者模式选项';
                        $param['statusType'] = '开发者模式选项';
                        $data['dev_mode'] = $param['dev_mode'];
                        break;
                    case 12:
                        $param['contentType'] = 35;
                        $param['remark'] = '录音功能管控';
                        $param['statusType'] = '录音功能管控';
                        $data['setting_recording'] = $param['setting_recording'];
                        break;
                    case 13:
                        $param['contentType'] = 36;
                        $param['remark'] = '文件管理功能管控';
                        $param['statusType'] = '文件管理功能管控';
                        $data['setting_file'] = $param['setting_file'];
                        break;
                    case 14:
                        $param['contentType'] = 37;
                        $param['remark'] = '图库功能管控';
                        $param['statusType'] = '图库功能管控';
                        $data['setting_picture'] = $param['setting_picture'];
                        break;
                    case 15:
                        $param['contentType'] = 38;
                        $param['remark'] = '音乐功能管控';
                        $param['statusType'] = '音乐功能管控';
                        $data['setting_music'] = $param['setting_music'];
                        break;
                    case 16:
                        $param['contentType'] = 39;
                        $param['remark'] = '拍照搜题开关';
                        $param['statusType'] = '拍照搜题开关';
                        $data['search_topic'] = $param['search_topic'];
                        break;
                    case 17:
                        $param['contentType'] = 40;
                        $param['remark'] = '电话白名单';
                        $param['statusType'] = '电话白名单';
                        $data['setting_phone'] = $setting_phone;
                        $data['setting_phones'] = $param['setting_phones'];
                        break;
                    case 18:
                        $param['contentType'] = 43;
                        $param['remark'] = '浏览器功能';
                        $param['statusType'] = '浏览器功能';
                        $data['setting_browser'] = $param['setting_browser'];
                        break;
                    case 19:
                        $param['contentType'] = 46;
                        $param['remark'] = '应用白名单';
                        $param['statusType'] = '应用白名单';
                        $data['setting_white_app'] = $param['setting_white_app'];
                        break;
                    case 20:
                        $param['contentType'] = 47;
                        $param['remark'] = '护眼总开关';
                        $param['statusType'] = '护眼总开关';
                        $data['is_eyecare'] = $param['is_eyecare'];
                        break;
                    case 21:
                        $param['contentType'] = 48;
                        $param['remark'] = '近距护眼开关';
                        $param['statusType'] = '近距护眼开关';
                        $data['eyecare_near'] = $param['eyecare_near'];
                        break;
                    case 22:
                        $param['contentType'] = 49;
                        $param['remark'] = '卧躺护眼开关';
                        $param['statusType'] = '卧躺护眼开关';
                        $data['eyecare_lie'] = $param['eyecare_lie'];
                        break;
                    case 23:
                        $param['contentType'] = 50;
                        $param['remark'] = '抖动护眼开关';
                        $param['statusType'] = '抖动护眼开关';
                        $data['eyecare_shake'] = $param['eyecare_shake'];
                        break;
                    case 24:
                        $param['contentType'] = 51;
                        $param['remark'] = '光线护眼开关';
                        $param['statusType'] = '光线护眼开关';
                        $data['eyecare_light'] = $param['eyecare_light'];
                        break;
                    case 25:
                        $param['contentType'] = 52;
                        $param['remark'] = '黑白护眼开关';
                        $param['statusType'] = '黑白护眼开关';
                        $data['eyecare_bw'] = $param['eyecare_bw'];
                        break;
                    case 26:
                        $param['contentType'] = 53;
                        $param['remark'] = '疲劳护眼开关';
                        $param['statusType'] = '疲劳护眼开关';
                        $data['eyecare_tired'] = $eyecare_tired;
                        $data['eyecare_tired_val'] = $eyecare_tired_val;
                        break;

                    default :
                        $param['contentType'] = 14;//默认
                        $param['remark'] = '电话功能管控';
                        $param['statusType'] = '电话功能管控';
                        $data['setting_call'] = $param['setting_call'];
                        break;
                }
                $data['code'] = 'success';
                $param['details'] = ToolUtil::handleSuccessOrError($data);
                LogUtil::addAdminLog($param);
                $jPush = LogUtil::addPushLog($param,$data);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'设置成功');
                }else{
                    return ToolUtil::returnJson(200,'设置成功,推送失败');
                }
            }else{
                $param['remark'] = '系统设置';
                $param['details'] = ToolUtil::handleSuccessOrError(['code'=>'error']);
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(300,'失败');
            }
        }catch (\Exception $exception){
            return ToolUtil::returnJson(500,'网络拥堵，请稍后再试',$exception->getMessage());
        }
    }

    /**
     * sn编号获取(应用管控)
     * 获取所有应用
     * @param $param
     * @return false|string
     */
    public static function getAppAll($param){

        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['a.value','p.teacher_id']);
        if(empty($parent['value'])){
            return ToolUtil::returnJson(301,'没有该设备信息');
        }

        if(!empty($param['type'])){

            $app = ParentAppModel::queryGet(['sn_value'=>$parent['value']],['id','app_package','app_name','app_img','is_lock','is_network','is_upgrade','address','visit']);
            $data = [];

            foreach ($app as $key => $value) {

                $res = DB::table('t_sys_app')->where(['app_package'=>$value['app_package']])->first(['id']);
                if($res){
                    $data[] = $value;
                }
                
            }

        }else{
            $data = ParentAppModel::queryGet(['sn_value'=>$parent['value']],['id','app_name','app_img','is_lock','is_network','is_upgrade','address','visit']);
        }

        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
        
    }

    /**
     * 卸载应用
     * @param $param
     * @return false|string
     */
    public static function appUninstall($param){
        return ToolUtil::returnJson(300,'已弃用');
        $app = AppInstallModel::queryWhereFirst(['i.id'=>$param['id']],['a.sn_value','a.app_package','i.parent_app_id']);
        if($app){
            DB::beginTransaction();
            $parent = ParentSnModel::queryWhereFirst(['a.value'=>$app['sn_value']],['p.teacher_id']);
            if($parent['teacher_id']){
                return ToolUtil::returnJson(301,'该设备授权老师，不能进行该操作');
            }
            $data['app_package'] = $app['app_package'];
            $int = AppInstallModel::delete(['id'=>$param['id']]);
            if($int){
                $param['status'] = 1;
                $param['type'] = 2;
                $param['batch'] = array($app['sn_value']);
                $param['title'] = 'OK';
                $param['contentType'] = 10;
                $param['remark'] = '删除应用';
                $param['statusType'] = '删除应用';
                $param['details'] = ToolUtil::handleSuccessOrError(['code'=>'success','app_package'=>$app['app_package']]);
                LogUtil::addAdminLog($param);
                $jPush = LogUtil::addPushLog($param,$data);
                if(in_array(200,$jPush)){
                    DB::commit();
                    return ToolUtil::returnJson(200,'应用卸载成功');
                }else{
                    DB::rollBack();
                    return ToolUtil::returnJson(300,'推送失败');
                }
            }
        }
        return ToolUtil::returnJson(301,'没有该应用信息');
    }


    /**
     * 获取年级名称
     * @return false|string
     */
    public static function querySnGrade(){
        $field = ['class_name'];
        $data = SnGradeModel::queryGet($field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * sn编号获取屏幕锁定和截图信息
     * @param $param
     * @return false|string
     */
    public static function querySnIdScreenshot($param){
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['p.id','a.is_screen_lock','a.value']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'该设备没有绑定');
        }
        $screenshot = ScreenshotModel::queryGet(['sn_value'=>$parent['value']],['img as image','create_time']);
        if($screenshot){
            foreach ($screenshot as $k=>$v){
                $screenshot[$k] = $v;
            }
        }else{
            $screenshot = [];
        }
        $data['id'] = $parent['id'];
        $data['is_screen_lock'] = $parent['is_screen_lock'];
        $data['screenshot'] = $screenshot;
        return ToolUtil::returnJson(200,'成功',$data);
    }

    /**
     * 设备发起截图
     * @param $param
     * @return false|string
     */
    public static function sendScreenshot($param){
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['a.value','p.teacher_id']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'该设备没有绑定');
        }
        if($parent['teacher_id']){
            return ToolUtil::returnJson(302,'该设备授权给老师');
        }
        $param['status'] = 1;
        $param['type'] = 2;
        $param['batch'] = array($parent['value']);
        $param['title'] = 'OK';
        $param['contentType'] = 26;
        $param['remark'] = '截图';
        $param['statusType'] = '截图';
        $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$parent['value']]);
        LogUtil::addAdminLog($param);
        $jPush = LogUtil::addPushLog($param);
        if(in_array(200,$jPush)){
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'设备不在线或没联网');
    }

    /**
     * 清除设备截图信息
     * @param $param
     * @return false|string
     */
    public static function deleteScreenshot($param){
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['a.value','p.teacher_id']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'不存在该设备信息');
        }
        if($parent['teacher_id']){
            return ToolUtil::returnJson(302,'该设备授权给老师');
        }
        $screenshot = ScreenshotModel::queryGet(['sn_value'=>$parent['value']],['id']);
        if(empty($screenshot)){
            return ToolUtil::returnJson(301,'没有信息,不需要清除');
        }
        if(ScreenshotModel::delete(['sn_value'=>$parent['value']])){
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 设备锁定屏幕和解锁屏幕
     * @param $param
     * @return false|string
     */
    public static function screenIsLock($param){
        DB::beginTransaction();
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['a.value','p.teacher_id']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'该设备没有绑定');
        }
        if($parent['teacher_id']){
            return ToolUtil::returnJson(302,'该设备授权给老师');
        }
        $param['is_screen_lock'] = (int)$param['is_screen_lock'];
        $rec = DB::table('t_admin_sn')->where(['value'=>$parent['value']])->first(['is_screen_lock']);
        if(empty($rec)){
            return ToolUtil::returnJson(302,'没有记录');
        }
        if($rec['is_screen_lock'] === $param['is_screen_lock']){

            if($param['is_screen_lock']){
                return ToolUtil::returnJson(302,'已经是锁定');
            }else{
                return ToolUtil::returnJson(302,'已经是解锁');
            }

        }

        if($param['is_screen_lock']){
            $param['remark'] = '锁定屏幕';
            $param['statusType'] = '锁定屏幕';
        }else{
            $param['remark'] = '解锁屏幕';
            $param['statusType'] = '解锁屏幕';
        }
        $int = AdminSnModel::edit(['value'=>$parent['value']],['is_screen_lock'=>$param['is_screen_lock']]);
        if($int){
            $param['status'] = 1;
            $param['type'] = 2;
            $param['batch'] = array($parent['value']);
            $param['title'] = 'OK';
            $param['contentType'] = 25;
            $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$parent['value']]);
            $jPush = LogUtil::addPushLog($param,['type'=>$param['is_screen_lock']]);
            if(in_array(200,$jPush)){
                LogUtil::addAdminLog($param);
                DB::commit();
                return ToolUtil::returnJson(200,'成功');
            }
            DB::rollBack();
            return ToolUtil::returnJson(300,'设备不在线或没联网');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * sn编号获取设备时间管控
     * @param $param
     * @return false|string
     */
    public static function queryTimeControl($param){
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['p.id','a.value','p.teacher_id']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'该设备没有绑定');
        }
        if($parent['teacher_id']){
            return ToolUtil::returnJson(302,'该设备授权给老师');
        }

        $type = (int)$param['type'];
        if($type > 1){
            return ToolUtil::returnJson(302,'类型参数错误');
        }

        $timeControl = TimeControlModel::queryFirst(['sn_value'=>$parent['value'],'day_type'=>$type],['id as tid','start_time','end_time']);

        $data['id'] = $parent['id'];
        $data['timeControl'] = $timeControl;
        if($data['timeControl']){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 增加时间管控
     * @param $param
     * @return false|string
     */
    public static function addTimeControl($param){
        DB::beginTransaction();
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['p.id','a.value','p.teacher_id']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'该设备没有绑定');
        }
        if($parent['teacher_id']){
            return ToolUtil::returnJson(302,'该设备授权给老师');
        }

        $type = (int)$param['type'];
        if($type > 1){
            return ToolUtil::returnJson(302,'类型参数错误');
        }

        $timeControl = TimeControlModel::queryFirst(['sn_value'=>$parent['value'],'day_type'=>$type],['id']);
        if($timeControl){
            return ToolUtil::returnJson(302,'不能重复添加');
        }
        $data = [
            'day_type'=>$type,
            'start_time'=>$param['startTime'],
            'end_time'=>$param['endTime'],
            'sn_value'=>$parent['value'],
            'create_time'=>time(),
            'admin_id'=>$param['userId']
        ];
        if(TimeControlModel::add($data)){
            $param['remark'] = '增加时间管控';
            $param['statusType'] = '增加时间管控';
            $param['status'] = 1;
            $param['type'] = 2;
            $param['batch'] = array($parent['value']);
            $param['title'] = 'OK';
            $param['contentType'] = 27;
            $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$parent['value']]);
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                DB::commit();
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,'成功');
            }
            DB::rollBack();
            return ToolUtil::returnJson(300,'设备不在线或没联网');
        }
    }

    /**
     * 修改时间管控
     * @param $param
     * @return false|string
     */
    public static function updateTimeControl($param){
        DB::beginTransaction();
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['p.id','a.value','p.teacher_id']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'该设备没有绑定');
        }
        if($parent['teacher_id']){
            return ToolUtil::returnJson(302,'该设备授权给老师');
        }

        $tid = (int)$param['tid'];
        $timeControl = TimeControlModel::queryFirst(['sn_value'=>$parent['value'],'id'=>$tid],['id']);
        if(empty($timeControl)){
            return ToolUtil::returnJson(301,'不存在该信息');
        }
        $data = [
            'start_time'=>$param['startTime'],
            'end_time'=>$param['endTime'],
            'update_time'=>time()
        ];
        if(TimeControlModel::edit(['sn_value'=>$parent['value'],'id'=>$tid],$data)){
            $param['remark'] = '修改时间管控';
            $param['statusType'] = '修改时间管控';
            $param['type'] = 2;
            $param['batch'] = array($parent['value']);
            $param['status'] = 1;
            $param['title'] = 'OK';
            $param['contentType'] = 27;
            $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$parent['value']]);
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                LogUtil::addAdminLog($param);
                DB::commit();
                return ToolUtil::returnJson(200,'成功');
            }
            DB::rollBack();
            return ToolUtil::returnJson(300,'设备不在线或没联网');
        }

        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除时间管控
     * @param $param
     * @return false|string
     */
    public static function deleteTimeControl($param){
        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['p.id','a.value','p.teacher_id']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'该设备没有绑定');
        }
        if($parent['teacher_id']){
            return ToolUtil::returnJson(302,'该设备授权给老师');
        }
        $tid = (int)$param['tid'];
        DB::beginTransaction();
        $timeControl = TimeControlModel::queryFirst(['sn_value'=>$parent['value'],'id'=>$tid],['id']);
        if(empty($timeControl)){
            return ToolUtil::returnJson(301,'不存在该信息');
        }
        if(TimeControlModel::delete(['sn_value'=>$parent['value'],'id'=>$tid])){
            $param['remark'] = '删除时间管控';
            $param['statusType'] = '删除时间管控';
            $param['batch'] = array($parent['value']);
            $param['type'] = 2;
            $param['status'] = 1;
            $param['contentType'] = 27;
            $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$parent['value']]);
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                LogUtil::addAdminLog($param);
                DB::commit();
                return ToolUtil::returnJson(200,'成功');
            }
            DB::rollBack();
            return ToolUtil::returnJson(300,'设备不在线或没联网');
        }

        return ToolUtil::returnJson(300,'失败');

    }


    /**
     * 远程管理
     * @param $param
     * @return false|string
     */
    public static function longRangeControl($param){

        $parent = ParentSnModel::queryWhereFirst(['p.id'=>$param['id']],['a.value','p.teacher_id']);
        if(empty($parent)){
            return ToolUtil::returnJson(301,'该设备没有绑定');
        }
        if($parent['teacher_id']){
            return ToolUtil::returnJson(302,'该设备授权给老师');
        }

        $type = (int)$param['type'];
        switch ($type) {
            case 1:
                $contentType = 22;
                $desc = '设备重启';
                break;
            case 2:
                $contentType = 17;
                $desc = '恢复出厂设置';
                break;
            
            default:
                $contentType = 0;
                $desc = '';
                break;
        }

        if($contentType === 0){
            return ToolUtil::returnJson(302,'类型错误');
        }

        $param['type'] = 2;
        $param['batch'] = array($parent['value']);
        $param['contentType'] = $contentType;
        $param['remark'] = $desc;
        $param['statusType'] = $desc;
        $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$parent['value']]);
        LogUtil::addAdminLog($param);
        $jPush = LogUtil::addPushLog($param);
        if(in_array(200,$jPush)){
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'设备不在线或没联网');

    }



}